package com.example.infowall02.mapper;

import com.example.infowall02.domain.CommentRequest;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface CommentMappper{
//增
    @Insert("INSERT INTO comment(article_id, user_id, content, create_time) VALUES (#{articleId}, #{userId}, #{content}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "commentId", keyColumn = "id")
    boolean insertComment(CommentRequest commentRequest);
//删
    // 根据评论 ID 删除评论
    @Delete("DELETE FROM comment WHERE comment_id = #{commentId}")
    int deleteComment(Integer commentId);
//查
    // 根据评论 ID 查询评论
    @Select("SELECT comment_id as commentId,article_id as articleId, user_id as userId, content, create_time as createTime FROM comment WHERE comment_id = #{commentId}")
    CommentRequest findCommentById(Integer commentId);
    // 根据用户 ID 和文章 ID 查看是否被评论过
    @Select("SELECT COUNT(*) FROM comment WHERE user_id = #{userId} AND article_id = #{articleId}")
    int countCommentsByUserAndArticle(int userId, int articleId);
    // 根据文章 ID 查询评论并分页
    @Select("SELECT comment_id as commentId,article_id as articleId, user_id as userId, content, create_time as createTime" +
            " FROM comment WHERE article_id = #{articleId} " +
            "order by create_time desc " +
            "limit #{pageNow},#{pageSize} ")
    List<CommentRequest> findCommentsByArticleId(Integer articleId,int pageNow,int pageSize);
    //根据文章id查询评论次数
    @Select("SELECT COUNT(*) FROM comment WHERE article_id = #{articleId}")
    int countCommentsByArticleId(Integer articleId);
//改
    @Update("UPDATE comment SET content = #{content}, create_time = #{createTime} WHERE comment_id = #{commentId}")
    int updateComment(String content, Integer commentId, Date createTime);


}
